package com.m2h.xiaoke.api;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;
import com.m2h.xiaoke.param.Constants;
import com.m2h.xiaoke.param.VoiceParamManager;

/**
 * @author lijh
 */
public class XunfeiSpeak{
    private static final String SIGN = "at " + XunfeiSpeak.class.getSimpleName() + ": ";
    private SpeechSynthesizer mTts;
    private Context context;
    private static XunfeiSpeak xunfeiSpeakInstance = null;
    /**
     * 初始化监听
     */
    private InitListener mTtsInitListener = new InitListener() {
        @Override
        public void onInit(int code) {
            LogUtils.d(SIGN + "mTtsInitListener onInit code" + code);
            if (code == ErrorCode.SUCCESS) {
                VoiceParamManager.setSpeakerParam(mTts, context);
            }
        }
    };

    private XunfeiSpeak() {

    }

    /**
     * 初始化语音合成
     */
    public void initSpeaker(Context context) {
        this.context = context;
        LogUtils.d(SIGN + "initSpeaker");
        if (SpeechSynthesizer.getSynthesizer() != null) {
            SpeechSynthesizer.getSynthesizer().destroy();
        }
        mTts = SpeechSynthesizer.createSynthesizer(context, mTtsInitListener);
    }

    /**
     * 反初始化
     */
    public void uninitSpeaker() {
        if(mTts!=null){
            mTts.destroy();
        }
    }

    /**
     * 开始语音合成
     *
     * @param content
     */
    public boolean doSpeak(String content, boolean needInterrupt) {
        if (mTts == null) {
            return false;
        }
        if (needInterrupt && mTts.isSpeaking()) {
            mTts.stopSpeaking();
        }
        if (!mTts.isSpeaking()) {
            int code = mTts.startSpeaking(content, new SynthesizerListener() {
                @Override
                public void onSpeakBegin() {
                }

                @Override
                public void onBufferProgress(int i, int i1, int i2, String s) {
                }

                @Override
                public void onSpeakPaused() {
                }

                @Override
                public void onSpeakResumed() {
                }

                @Override
                public void onSpeakProgress(int i, int i1, int i2) {
                }

                @Override
                public void onCompleted(SpeechError speechError) {
                }

                @Override
                public void onEvent(int i, int i1, int i2, Bundle bundle) {
                }
            });
            LogUtils.d(SIGN + "doSpeak content:" + content + ",code =" + code);
            return code == ErrorCode.SUCCESS;
        }
        return false;
    }
    /**
     * 是否正在说话
     *
     * @return
     */
    public boolean isSpeaking() {
        if (null == mTts) {
            return false;
        }
        return mTts.isSpeaking();
    }
    public void stopSpeak(){
        if(null == mTts) {
            return;
        }
        mTts.stopSpeaking();
    }

    public static XunfeiSpeak getInstance() {
        if (xunfeiSpeakInstance == null) {
            synchronized (XunfeiSpeak.class) {
                if (xunfeiSpeakInstance == null) {
                    xunfeiSpeakInstance = new XunfeiSpeak();
                }
            }
        }
        return xunfeiSpeakInstance;
    }
}
